﻿using System;
using System.Collections.Generic;
using System.Text;
using EnCor.Config;
using EnCor.ObjectBuilder;
using System.Web.Security;
using System.Configuration;

namespace EnCor.Security
{
    [Assembler(typeof(MembershipAuthenticationProviderAssembler))]
    public class MembershipAuthenticationProviderConfig : AuthenticationProviderConfig
    {
        public const string ProviderNameString = "providerName";
        /// <summary>
        /// Name of Membership Provider
        /// </summary>
        [ConfigurationProperty(ProviderNameString)]
        public string ProviderName
        {
            get
            {
                return (string)this[ProviderNameString];
            }
        }

        [ConfigurationProperty("identityType", DefaultValue="username")]
        public string IdentityTypeString
        { 
            get
            {
                return (string)this["identityType"];
            }
        }

        public MembershipAuthenticationProvider.MembershipIdentityType IdentityType
        {
            get
            {
                switch( IdentityTypeString.ToLower() )
                {
                    case "username":
                    case "name":
                        return MembershipAuthenticationProvider.MembershipIdentityType.Username;
                    case "provideruserkey":
                    case "id":
                        return MembershipAuthenticationProvider.MembershipIdentityType.ProviderUserKey;
                    default:
                        throw new EnCorException("The \"identityType\" configured for MembershipAuthenticationProvider should be in he following values: id, username, name, provideruserkey");
                }
            }
        }
    }



    public class MembershipAuthenticationProviderAssembler : AuthenticationProviderAssembler
    {

        public override IAuthenticationProvider Assemble(IBuilderContext context, 
            AuthenticationProviderConfig objectConfiguration)
        {
            MembershipAuthenticationProviderConfig config = objectConfiguration as MembershipAuthenticationProviderConfig;
            if (config == null)
            {
                throw new ConfigurationErrorsException();
            }
            
            MembershipProvider membershipProvider = null;
            if (string.IsNullOrEmpty(config.ProviderName))
            {
                membershipProvider = Membership.Provider;
            }
            else
            {
                membershipProvider = Membership.Providers[config.ProviderName];
            }

            if (membershipProvider == null)
            {
                throw new ConfigurationErrorsException();
            }
            MembershipAuthenticationProvider provider = new MembershipAuthenticationProvider(membershipProvider, config.IdentityType);
            provider.Tag = config.Tag;
            return provider;
        }
    }
}
